/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import java.util.Date;
import java.util.List;
import org.crystalrange.dao.UserDao;
import org.crystalrange.model.User;
import org.crystalrange.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;
    private UserService userService;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public User create(String username) {
        
        User currentUser = this.userService.findByUsername(username);
        
        User user = new User();
        user.setCreatedBy(currentUser);
        user.setCreatedDate(new Date());
        
        return user;
    }
    
    public boolean save(User user, String username) {
        
        User currentUser = this.userService.findByUsername(username); 
        
        user.setUpdatedBy(currentUser);
        user.setUpdatedDate(new Date());
        
        return this.userDao.save(user);        
    }

    public boolean remove(User user) {
        return this.userDao.remove(user);
    }

    public User find(Long id) {
        return this.userDao.find(id);
    }

    public User findByUsername(String username) {
        
        Search searchUser = new Search(User.class);
        searchUser.addFilterEqual("username", username);
        
        return this.userDao.searchUnique(searchUser);
    }

    public List<User> findAll() {
        return this.userDao.findAll();
    }

    public SearchResult<User> searchAndCount(Search search) {
        return this.userDao.searchAndCount(search);
    }

    public List<User> search(Search search) {
        return this.userDao.search(search);
    }
    
    public User searchUnique(Search search) {
        return this.userDao.searchUnique(search);
    }
}
